//获取地址栏参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    // 解决获取中文乱码
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}

/**
 * 判断值是否不为空
 * @param {Object} val
 */
function isNotNull(val) {
    if (val !== 'undefined' && val !== "" && val != null) {
        return true
    }
    return false
}

/**
 * 生成uuid
 */
function guid() {
    let uuid = ''
    for (let i = 0; i < 8; i++) {
        if (i == 0) {
            uuid = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
        } else {
            uuid += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
        }
    }
    return uuid
}

/**
 * 生成一个用不重复的ID(可指定位数和前缀)
 */
function getUuiD(length, preffix) {
    //timestamp为当前时间戳
    let timestamp = (new Date()).getTime()
    length = length - 13;
    let str = "";
    let str_arr = [
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
    for (let i = 0; i < length; i++) {
        let r = parseInt(Math.random() * 9);//生成0-62之间的随机数
        str += str_arr[r];
    }
    let res_str = preffix + str + timestamp
    return res_str;
}

/**
 * 通用下载Excel
 * 后端直接返回文件流即可
 */
function downloadExcel(url, params, fileName, method) {
    // 默认是GET
    if (method !== 'GET' && method !== 'POST') {
        method = 'GET'
    }
    // 使用XMLHttpRequest 发送请求去下载
    var xhr = new XMLHttpRequest();
    xhr.open(method, url, true);
    if (method === 'GET') {
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    } else if (method === 'POST') {
        xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
    } else {
        return;
    }
    xhr.responseType = 'blob';
    xhr.onload = function () {
        if (xhr.status === 200) {
            const blob = new Blob([xhr.response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
            const elink = document.createElement('a')
            elink.download = fileName ?? '数据导出.xlsx'
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href)
            document.body.removeChild(elink)
        } else {
            console.error('Request failed.  Returned status of ' + xhr.status);
        }
    };
    // 组装参数
    if (method === 'GET') {
        var formData = Object.keys(params).map(function (key) {
            return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
        }).join('&');
        xhr.send(formData);
    } else if (method === 'POST') {
        xhr.send(JSON.stringify(params));
    }
}

function hideStr(str, num) {
    if (isNotNull(str)) {
        return str.length > num ? str.slice(0, num) + '.....' : str;
    } else {
        return '';
    }
}

function copyToClipboard(text) {
    // 创建临时的文本输入框元素
    var tempInput = document.createElement('textarea');
    tempInput.value = text;

    // 将文本输入框添加到文档中
    document.body.appendChild(tempInput);

    // 选择文本
    tempInput.select();
    tempInput.setSelectionRange(0, 99999); // 兼容部分移动设备

    // 复制文本到剪贴板
    document.execCommand('copy');

    // 移除临时的文本输入框
    document.body.removeChild(tempInput);
}
